package com.eight.paper.admin.util;

import com.eight.common.utils.StringUtils;
import com.eight.paper.admin.domain.model.QuestionTemplate;
import com.eight.paper.admin.domain.model.enums.QuestionTypeEnum;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 试题导出pdf处理
 */
public class PdfUtil {

    /**
     * @param questions
     * @param questionList
     * @param questionNo
     * @return
     */
    public static void createQuestion(List<String> questions,List<QuestionTemplate> questionList, AtomicInteger questionNo) {
        if (CollectionUtils.isNotEmpty(questionList)) {
            for (int i = 0; i < questionList.size(); i++) {
                String questionType = questionList.get(0).getQuestionType();
                if (StringUtils.isNotBlank(questionType)) {
                    questionType = questionType.toUpperCase();
                }
                QuestionTemplate question = questionList.get(i);
                //处理题目内容
                if (i == 0) {
                   createTitle(questions,question,questionNo,questionType);
                } else {
                    createOptions(questions,question,questionType);
                }
            }
        }
    }

    /**
     * 生成题干内容
     *
     * @param questions
     * @param question
     */
    public static Map<String, Object> createTitle(List<String> questions, QuestionTemplate question, AtomicInteger questionNo,String questionType) {
        Map<String, Object> datas = new HashMap<>();
        String content = question.getTitle();
        List<String> imgs = matcherImgs(content);
        if (imgs.size() > 0) {
            for(String img:imgs) {
                String newImg = img;
                if(img.endsWith("\">")){
                    newImg = img.replace("\">","\"/>").replace("/dev-api/profile", "http://localhost:81/dev-api/profile");
                }
                if(img.endsWith("'>")){
                    newImg = img.replace("'>","'/>").replace("/dev-api/profile", "http://localhost:81/dev-api/profile");;
                }
                content = content.replace(img, newImg);
            }
        }
        String answer = "";
        content = content.replace("%", "%%");
        QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.fromName(questionType);
        if(QuestionUtil.checkQuestionType(questionTypeEnum.getCode())) {
            if(QuestionTypeEnum.B1==questionTypeEnum){
                content = String.format("%s、（%s）", questionNo.incrementAndGet(), questionType);
                questions.add(content);
                content = String.format("%s、%s", question.getAnswer(), question.getTitle());
            }else if(QuestionTypeEnum.A3A4Single==questionTypeEnum||QuestionTypeEnum.A3A4Single==questionTypeEnum){
                content = String.format("%s、（%s）" + content, questionNo.incrementAndGet(), questionType);
            }else {
                content = String.format("%s、（%s）" + content + "（ %s ）", questionNo.incrementAndGet(), questionType, question.getAnswer());
            }
            questions.add(content);
        }else  {
            content = String.format("%s、（%s）" + content, (questionNo.incrementAndGet()), questionType);
            answer = "参考答案：" ;
            questions.add(content);
            questions.add(answer);
        }
        return datas;
    }

    /**
     * 生成题目内容
     *
     * @param questions
     * @param question
     */
    public static void createOptions(List<String> questions, QuestionTemplate question,String questionType) {
        QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.fromName(questionType);
        if(QuestionUtil.checkQuestionType(questionTypeEnum.getCode())) {
            String content = question.getAnswer() + "、" + question.getTitle();
            if(StringUtils.isNotBlank(question.getQuestionType())){
                content = String.format("（%s）、%s（%s）",question.getQuestionType(),question.getTitle(),question.getAnswer());
            }
            questions.add(content);
        }else {
            String content = question.getTitle() + "、" + question.getAnswer();
            questions.add(content);
        }
    }

    /**
     * 获取内容中的图片地址
     *
     * @param content
     * @return list
     */
    public static List<String> matcherImgs(String content) {
        List<String> srcList = new ArrayList<>();
        Pattern p = Pattern.compile("<(img|IMG)(.*?)(>|></img>|/>)");
        Matcher matcher = p.matcher(content);
        boolean hasPic = matcher.find();
        if (hasPic)
        {
            while (hasPic)
            {
                String group = matcher.group(0);
                srcList.add(group);
                hasPic = matcher.find();
            }
        }
        return srcList;
    }
}
